﻿using System;
using Cms.Web.Framework.Controls;
using Cms.Application.Admin.Controllers;
using Cms.Application.Admin.Views;
//using Brettle.Web.NeatUpload;

namespace Cms.Web.UI.Admin
{

    public partial class Upload : AdminPage, IUploadView
    {

        private UploadController _controller;


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _controller = new UploadController(this);
            this.HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                //_controller.Initialize();
                HeadingIcon = litIcon.Text;
                hlCancel.NavigateUrl = "Admin/Media.aspx";
                if (!string.IsNullOrEmpty(Request.QueryString["browseitemdata"]))
                    hlCancel.NavigateUrl += "?browseitemdata=" + Server.UrlEncode(Request.QueryString["browseitemdata"]);
            }
            string script = "function displayOverwriteConfirmation(existingFiles) { " +
                                "document.getElementById('lblExistingFiles').innerHTML = existingFiles;" +
                                "cms.Dialogs.Confirm(validationCallback, '" + GetText(this, "OverwriteHeader") + "', null, '" + GetText(this, "OverwriteOK") + "', '" + GetText(this, "OverwriteCancel") + "', true, null, 'overwriteWarning');" +
                            "}";
            iLib.Web.UI.ClientScriptProxy.Current.RegisterClientScriptBlock(this, this.GetType(), "validate", script, true);
            ifrCheckFiles.Attributes.Add("src", "Admin/UploadCheck.aspx");
        }


        protected void lbCheckUpload_Click(object sender, EventArgs e)
        {
            // This method will only fire if the Upload button is clicked when the selected files list is empty
            string redirectUrl = "Media.aspx";
            if (!string.IsNullOrEmpty(Request.QueryString["browseitemdata"]))
                redirectUrl += "?browseitemdata=" + Server.UrlEncode(Request.QueryString["browseitemdata"]);
            Response.Redirect(redirectUrl);
        }


        protected void lbStartUpload_Click(object sender, EventArgs e)
        {
            foreach (UploadedFile file in multiFileId.Files)
            {
                //_controller.SaveUploadedFile();
            }
            string redirectUrl = "Media.aspx";
            if (!string.IsNullOrEmpty(Request.QueryString["browseitemdata"]))
                redirectUrl += "?browseitemdata=" + Server.UrlEncode(Request.QueryString["browseitemdata"]);
            Response.Redirect(redirectUrl);
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Upload.xml"; }
        }


        public void SetFolderName(string name)
        {
            litFolder.Text = name;
        }

    }

}
